package yunwo.cn.yzstation.utils;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class SetListViewHeightUtil {
    	
	private static final String TAG = "SetListViewHeightUtil";

	/**
	 * 动态设置ListView的高度
	 * @param listView
	 */
	public static void setListViewHeightBasedOnChildren(ListView listView) {
//	    if(listView == null) return;

	    ListAdapter listAdapter = listView.getAdapter();
	    if (listAdapter == null) { 
	        // pre-condition 
	        return; 
	    } 

	    int totalHeight = 0; 
	    for (int i = 0, len = listAdapter.getCount();i<len; i++) { 
	        View listItem = listAdapter.getView(i, null, listView);
	        listItem.measure(0, 0); 
	        totalHeight += listItem.getMeasuredHeight();
	    } 
//	    LogUtil.d(TAG, "totalHeight----"+totalHeight);

	    ViewGroup.LayoutParams params = listView.getLayoutParams();
	    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
	    listView.setLayoutParams(params); 
	}
}


